<?php
namespace com\mvc\orm\dao;
class PDODB implements DBInterfaz{
	private static $instance;
	private $db;
	private $statement;
	private $where;
	private $bind_params;
	private $groupBy;
	private $columns;
	private $tables;
	private static $iterator_names = 0;
	private $forcedQuery;
	
	public function __construct(){
		$this->db = new \PDO(DB_TYPE.":host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
		//$this->db = "Conexion PDO";
	}
	
	public static function singleton(){
		if (!self::$instance instanceof self)
		{
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	public function getDB(){
		return $this->db;
	}
	
	public function __clone(){
		trigger_error("Clonación no permitida",E_USER_ERROR);
	}
	
	public function forcedQuery($query){
		$this->forcedQuery = $query;
	}
	
	public function addBindingParams($binding){
		if (!isset($this->bind_params)){
			$this->bind_params = array();
		}
		if (is_array($binding)){
			foreach ($binding as $value){
				if (isset($value['type'])){
					$this->bind_params[] = array('key'=>$value['key'],'value'=>$value['value'],'type'=>$value['type']);
				}else{
					$this->bind_params[] = array('key'=>$value['key'],'value'=>$value['value']);
				}
			}
		}
	}
	
	public function addWhere(){
		$numargs = func_num_args();
		
		if (!$this->where){
			$this->where = array();
		}
		if ($numargs>0){
			$args = func_get_args();
			foreach ($args as $value){
				if (is_array($value)){
					foreach ($value as $where){
						//$this->setDataGenericObject($where);
					}
				}else if (is_object($value)){
					//$this->setDataGenericObject($where);
				}
			}
		}
	}
	public function addColumn(){
		return "addColumn";
	}
	public function addTables(){
		return "addTables";
	}
	public function addGroupBy(){
	
	}
	public function query(){
		if ($this->forcedQuery){
			$this->statement = $this->db->prepare($this->forcedQuery);
			foreach ($this->bind_params as $key=>$value){
				if (isset($value['type'])){
					$this->statement->bindParam($value['key'],$value['value'],$value['type']);
				}else{
					$this->statement->bindParam($value['key'],$value['value']);
				}
			}
		}else{
			
		}
		$this->statement->execute();
		$result = $this->statement->fetchAll();
		return $result;
	}
	public function insert(){
		return "insert";
	}
	public function update(){
		return "update";
	}
	public function delete(){
		return "delete";
	}
	
	public function reset(){
		unset($this->bind_params);
		unset($this->statement);
		unset($this->bind_params);
		unset($where);
		unset($groupBy);
		unset($columns);
		unset($tables);
	}
	
	public function closeConnection(){
		$this->bind_params = null;
		$this->statement = null;
		$this->db = null;
	}
	public function closeStatement(){
		
	}
	
	private function fillWhere(&$value){
		if ($value){
			if ($value->getNames()){
				if (!$this->bind_params){
					$this->bind_params = array();
				}
				$bind_param = array();
				if ($value->getType()){
					$bind_param["type"] = $value->getType(); 
				}
				if ($value->getName()){
					$bind_param["name"] = $value->getName();
				}
			}
			if ($value->getValue()){
				if ($this->where){
					$this->where = "";
				}
				$this->tables = $value->getColumn();
			}
		}
	}
}